﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="MetroContractSample.SearchResultsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MetroContractSample"
    xmlns:common="using:MetroContractSample.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:ExtensionType="Search">

    <UserControl.Resources>
        <CollectionViewSource x:Name="resultsViewSource" Source="{Binding Results}"/>
        <CollectionViewSource x:Name="filtersViewSource" Source="{Binding Filters}"/>
        <common:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <!-- TODO: キー AppName が App.xaml で宣言されている場合は、この行を削除します -->
        <!--<x:String x:Key="AppName">マイ アプリケーション</x:String>-->
    </UserControl.Resources>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid x:Name="layoutRoot" Background="{StaticResource ApplicationPageBackgroundBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Back button, page title and subtitles-->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Grid.Column="0" Click="GoBack" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Grid.Column="1" Text="{StaticResource AppName}" Style="{StaticResource PageHeaderTextStyle}"  />
            <TextBlock x:Name="resultText" Grid.Column="2" Text="Results for&#x00a0;" Style="{StaticResource PageSubheaderTextStyle}"/>
            <TextBlock x:Name="queryText" Grid.Column="3" Text="{Binding QueryText}" Style="{StaticResource PageSubheaderTextStyle}"/>
        </Grid>

        <!--
            The body of the page in most view states uses an items controls to create multiple radio buttons
            for filtering above a horizontal scrolling grid of search results
        -->
        <Grid x:Name="typicalPanel" Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <ItemsControl
                x:Name="filtersItemsControl"
                ItemsSource="{Binding Source={StaticResource filtersViewSource}}"
                Visibility="{Binding ShowFilters, Converter={StaticResource BooleanToVisibilityConverter}}"
                Margin="120,-3,120,30">

                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <RadioButton
                            Content="{Binding Description}"
                            GroupName="Filters"
                            IsChecked="{Binding Active, Mode=TwoWay}"
                            Checked="Filter_Checked"
                            Margin="0,0,40,0"
                            Style="{StaticResource TextRadioButtonStyle}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

            <ScrollViewer
                x:Name="resultsGridScrollViewer"
                Grid.Row="1"
                AutomationProperties.AutomationId="ResultsGridScrollViewer"
                Margin="0,-4,0,0"
                Style="{StaticResource HorizontalScrollViewerStyle}">

                <GridView
                    x:Name="resultsGridView"
                    AutomationProperties.AutomationId="ResultsGridView"
                    AutomationProperties.Name="Search Results"
                    ItemsSource="{Binding Source={StaticResource resultsViewSource}}"
                    ItemTemplate="{StaticResource StandardSmallIcon300x70ItemTemplate}"
                    Margin="116,0,116,46"/>
            </ScrollViewer>
        </Grid>

        <!--
            The body of the page when snapped uses a combo box to select a filter above a
            vertical scrolling list of search results
        -->
        <Grid x:Name="snappedPanel" Grid.Row="1" Visibility="Collapsed">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <ComboBox
                ItemsSource="{Binding Source={StaticResource filtersViewSource}}"
                Visibility="{Binding ShowFilters, Converter={StaticResource BooleanToVisibilityConverter}}"
                Margin="20,0,20,20"
                SelectionChanged="Filter_SelectionChanged">
            </ComboBox>

            <ScrollViewer Grid.Row="1" Margin="16,0,0,0" Style="{StaticResource VerticalScrollViewerStyle}">
                <ListView
                    x:Name="resultsListView"
                    ItemsSource="{Binding Source={StaticResource resultsViewSource}}"
                    ItemTemplate="{StaticResource StandardSmallIcon70ItemTemplate}"
                    Margin="0,0,0,60"/>
            </ScrollViewer>
        </Grid>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup>
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="filtersItemsControl" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="100,-3,90,30"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="resultsGridView" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="96,0,86,56"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!--
                    The back button and title have different styles when snapped in addition to more extensive changes:
                    * The query text becomes the page header
                    * A different representation for filter selection and search results is used
                -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="queryText" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="resultText" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="typicalPanel" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="snappedPanel" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
